package com.mogist.hqc.module.personal.ui

import android.content.Intent
import android.os.Build
import android.view.View
import android.view.WindowManager
import butterknife.OnClick
import butterknife.Optional
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.entitys.response.LoginVo
import com.mogist.hqc.module.personal.contract.BindMobileContract
import com.mogist.hqc.module.personal.presenter.BindMobilePresenter
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import com.mogist.hqc.util.Util
import com.mogist.hqc.view.PhoneCode
import kotlinx.android.synthetic.main.activity_bindmobile.*
import talex.zsw.basecore.util.*
import talex.zsw.basecore.view.dialog.sweetalertdialog.SweetAlertDialog
import talex.zsw.basecore.view.other.RxToast
import java.util.*

/**
 * 作用：绑定手机
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class BindMobileActivity : BaseMVPActivity<BindMobileContract.Presenter>(), BindMobileContract.View
{
    var mobile = ""
    override fun initArgs(intent: Intent?)
    {
    }

    override fun initView()
    {
        setContentView(R.layout.activity_bindmobile)
        mPresenter = BindMobilePresenter(this)
        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            //透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

    override fun initData()
    {
        mEtTel.setAfterChangedListener { s ->
            mTvSend.isEnabled = RegTool.isMobileSimple(s)
        }

        mPhoneCode.setOnInputListener(object : PhoneCode.OnInputListener
                                      {
                                          override fun onSucess(code: String)
                                          {
                                              LogTool.nd("code = $code")
                                              bindMobile(code)
                                          }

                                          override fun onInput()
                                          {
                                              LogTool.nd("onInput")
                                          }
                                      })
    }

    override fun onBackPressed()
    {
        exit()
    }

    @Optional
    @OnClick(R.id.mTvSend, R.id.mTvCode, R.id.mIvBack)
    fun onViewClicked(view: View)
    {
        if (!isFastClick)
        {
            when (view.id)
            {
                R.id.mTvSend ->
                {
                    mobile =
                            mEtTel.text.toString()
                                    .trim()
                    val body = BaseModel()
                    body.mobile = mobile
                    mPresenter.getData(HttpDto(Constant.LOGIN_SEND, body).setErrorTitle("发送验证码失败"))
                }
                R.id.mTvCode ->
                {
                    val body = BaseModel()
                    body.mobile = mobile
                    mPresenter.getData(HttpDto(Constant.LOGIN_SEND, body).setErrorTitle("发送验证码失败"))
                }
                R.id.mIvBack ->
                {
                    exit()
                }
            }
        }
    }

    private fun exit()
    {
        showDialog(SweetAlertDialog.WARNING_TYPE, "是否放弃绑定？", "", "确定", "取消", {
            finish()
        }, null)
    }

    override fun sendSuccess()
    {
        mLLTel.visibility = View.GONE
        mLLCode.visibility = View.VISIBLE
        mTvCode.isClickable = false
        mTvCode.setTextColor(ColorTool.getColorById(R.color.text_black3))
        Tool.countDown(mTvCode,
                       60 * 1000,
                       1000,
                       DataTool.getString(R.string.login_count_down),
                       DataTool.getString(R.string.login_resend)) {
            mTvCode.isClickable = true
            mTvCode.setTextColor(ColorTool.getColorById(R.color.text_orange))
        }
        mPhoneCode.showSoftInput()
        val phone = getResources().getString(R.string.login_code_send)
        mTvPhone.text = String.format(phone, mobile)
    }

    override fun onDestroy()
    {
        Tool.cancelCountDown()
        super.onDestroy()
    }

    // --------------- 绑定手机 ---------------
    private fun bindMobile(code: String)
    {
        val body = BaseModel(true)
        body.mobile = mobile
        body.code = code
        mPresenter.getData(HttpDto(Constant.Bind_Mobile, body).setErrorTitle("绑定手机失败"))
    }

    override fun bindSuccess(data: LoginVo)
    {
        RxToast.success("绑定成功")
        data.birthStringDay = Util.getTimeStr(data.birthDay)
        SpTool.saveString(Constant.STR_TOKEN, data.appToken)
        SpTool.saveLong(Constant.LONG_TOKEN, Date().time)
        SpTool.saveString(Constant.STR_MOBILE, data.mobile)
        SpTool.saveString(Constant.STR_USER_PHOTO, data.headImg)
        SpTool.saveString(Constant.STR_USER_NAME, data.userName)
        SpTool.saveObject(Constant.OBJ_LOGIN, data)
        finish()
    }
}